//
//  XMLogFormatter.m
//  XunMi
//
//  Created by MacBook on 2024/2/18.
//

#import "XMLogFormatter.h"
#import <libkern/OSAtomic.h>

static NSString *const KDateFormatString = @"yyyy-MM-dd HH:mm:ss";

@implementation XMLogFormatter

- (NSString *)stringFromDate:(NSDate *)date {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    int32_t loggerCount = OSAtomicAdd32(0, &atomicLoggerCount);
#pragma clang diagnostic pop

    if (loggerCount <= 1) {
        // Single-threaded mode.
        if (threadUnsafeDateFormatter == nil) {
            threadUnsafeDateFormatter = [[NSDateFormatter alloc] init];
            [threadUnsafeDateFormatter setDateFormat:KDateFormatString];
        }

        return [threadUnsafeDateFormatter stringFromDate:date];
    } else {
        // Multi-threaded mode.
        // NSDateFormatter is NOT thread-safe.

        NSString *key = @"MyCustomFormatter_NSDateFormatter";

        NSMutableDictionary *threadDictionary = [[NSThread currentThread] threadDictionary];
        NSDateFormatter *dateFormatter = [threadDictionary objectForKey:key];

        if (dateFormatter == nil) {
            dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:KDateFormatString];

            [threadDictionary setObject:dateFormatter forKey:key];
        }

        return [dateFormatter stringFromDate:date];
    }
}

#pragma mark - DDLogFormatter
- (NSString *)formatLogMessage:(DDLogMessage *)logMessage {
    NSString *logLevel; // 日志等级
    switch (logMessage->_flag) {
        case DDLogFlagError:
            logLevel = @"Error";
            break;
        case DDLogFlagWarning:
            logLevel = @"Warning";
            break;
        case DDLogFlagInfo:
            logLevel = @"Info";
            break;
        case DDLogFlagDebug:
            logLevel = @"Debug";
            break;
        default:
            logLevel = @"Verbose";
            break;
    }

    //   NSString *dateAndTime = [self stringFromDate:(logMessage.timestamp)]; // 日期和时间
    NSString *logFileName = logMessage->_fileName; // 文件名
    NSString *logFunction = logMessage->_function; // 方法名
    NSUInteger logLine = logMessage->_line; // 行号
    NSString *logMsg = logMessage->_message; // 日志消息

    // 日志格式：日期和时间 文件名 方法名 : 行数 <日志等级> 日志消息
    //   return [NSString stringWithFormat:@"%@ %@ %@ : %lu <%@> %@", dateAndTime, logFileName, logFunction, logLine, logLevel, logMsg];
    return [NSString stringWithFormat:@"<%@> [%lu行]:\n%@\n%@  \n%@", logLevel, logLine, logFileName, logFunction, logMsg];
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (void)didAddToLogger:(id<DDLogger>)logger {
    OSAtomicIncrement32(&atomicLoggerCount);
}

- (void)willRemoveFromLogger:(id<DDLogger>)logger {
    OSAtomicDecrement32(&atomicLoggerCount);
}
#pragma clang diagnostic pop

@end
